//
//  SISSTenantRegistSuccessViewController.m
//  SISSAPP
//
//  Created by 袁应荣 on 2019/10/31.
//  Copyright © 2019 优房网. All rights reserved.
//

#import "SISSTenantRegistSuccessViewController.h"
#import "SISSCommonRentCell.h"
#import "SISSNetworkingManager+Home.h"
#import "SISSJsonRequestNetworkingManager+Registration.h"
#import "SISSRecommendTenantViewController.h"

@interface SISSTenantRegistSuccessViewController ()<SISSCommonRentCellDelegate>


@property (strong, nonatomic) NSArray<SISSRentOutModel *> *rentOutArr;

@end

@implementation SISSTenantRegistSuccessViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"租客登记成功";
    
    self.tipStr = @"以下出租房是该求租的同类型房源，您可以点击推荐租客给下面房源的房东。";
    [self.tableView registerClass:[SISSCommonRentCell class] forCellReuseIdentifier:@"tenantCellReuseID"];
//    [self requestData];
}

- (void)backReccommend {
   for (UIViewController *controller in self.navigationController.viewControllers) {
       if ([controller isKindOfClass:[SISSRecommendTenantViewController class]]) {
           [self.navigationController popToViewController:controller animated:YES];
       }
   }
}

- (void)setArrId:(NSString *)arrId {
    _arrId = arrId;
    [self requestData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.rentOutArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    SISSCommonRentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tenantCellReuseID" forIndexPath:indexPath];
    cell.delegate = self;
    cell.rentOutModel = self.rentOutArr[indexPath.row];
    
    return cell;
}

#pragma mark - SISSCommonRentCellDelegate
- (void)commonRentCellRecommendButtonDidClick:(UIButton *)button {
    
    SISSCommonRentCell *cell = (SISSCommonRentCell *)button.nextResponder.nextResponder;
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    SISSRentOutModel *model = self.rentOutArr[indexPath.row];
    
   [kNetworkManager requestInsertRecommendTenantWithAlrId:model.ID arrId:self.arrId arbpId:@"-1" rentType:2 success:^(id responseObject) {
       
       if ([responseObject[@"code"] integerValue] == 200) {
           NSLog(@"%@",responseObject[@"message"]);
           
           [[NSOperationQueue mainQueue] addOperationWithBlock:^{
               
               [SISSTools showSVSuccessTitle:@"推荐成功" andTime:2 andColor:YES];
               [self requestData];
           }];
           
       }
       
   } failed:^(NSError *error) {
       
       [[NSOperationQueue mainQueue] addOperationWithBlock:^{
           
           [SISSTools showSVErrorTitle:@"推荐失败" andTime:2 andColor:YES];
       }];
   }];
}

- (void)requestData {
    
    [kJsonRequestNetworkingManager requestTenantRecommendRentOutWithArrId:self.arrId success:^(NSArray<SISSRentOutModel *> *rentOutResources) {
        
        self.rentOutArr = rentOutResources;
        [self.tableView reloadData];
        
    } failed:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}

@end
